package com.fzh.study.sharedingjdbcdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.study.sharedingjdbcdemo.mapper.UserMapper;
import com.fzh.study.sharedingjdbcdemo.pojo.User;
import com.fzh.study.sharedingjdbcdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 *
 * @author fzh
 * @since 2020-09-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {



    @Override
    public String addUser() {
        User user = new User();
        for (int i = 0; i < 30; i++) {
            user.setId((long)i);
            user.setUserId((long)i);
            Random r = new Random();
            user.setOrderId((long)r.nextInt(100));
            user.setNickName("nick_"+i);
            user.setPassWord("pwd_"+i);
            user.setUserName("name_"+i);
            baseMapper.insert(user);
        }
        return "success";
    }

    @Override
    public String getUserList() {
        QueryWrapper<User> userWrapper = new QueryWrapper();
        userWrapper.orderByDesc("user_id");
        return JSONObject.toJSONString(baseMapper.selectList(userWrapper));
    }


    @Override
    public String delUser(Long user_id) {
        QueryWrapper<User> userWrapper = new QueryWrapper();
        userWrapper.eq("user_id",user_id);
        return baseMapper.delete(userWrapper)>0?"success":"fail";
    }
}